/* utility function to write n bytes to a descriptor. 
 
 Adapted from:
 
 Unix Network Programming, Volume 1: 
	The Sockets Networking API , 3rd Edition,
	
	by W. Richard Stevens,  Bill Fenner, Andrew M. Rudoff,
	Addison-Wesley, 2004
 */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>

ssize_t _writen(int fd, const void *vptr, size_t n)
{
	size_t		nleft;
	ssize_t		nwritten;
	const char	*ptr;

	ptr = vptr;
	nleft = n;
	while (nleft > 0) {
		if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
			if (errno == EINTR)
				nwritten = 0;		/* and call write() again */
			else
				return(-1);			/* error */
		}
		nleft -= nwritten;
		ptr   += nwritten;
	}
	return(n);
}

void writen(int fd, void *ptr, size_t nbytes)
{
	if (_writen(fd, ptr, nbytes) != nbytes)
		perror("writen error\n");
}
